/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.entity;

import java.util.List;
import xperiment.metaphor.model.*;
import metaphor.service.ServiceException;
import metaphor.persistence.filter.Query;
import metaphor.persistence.filter.SimpleExpression;

/**
 * Population group
 * @author Mark Ashworth
 * @version 1.0.0
 */
//@Entity
//@Table(name="population_group")
public class PopulationGroup extends DomainObject implements Persistent, Named {

    /* The unique id of the population group */
    //@Id @GeneratedValue(strategy=GenerationType.AUTO)
    //@Column(name = "population_group_id", unique = true, nullable = false)
    private Long id = null;
    
    /* The name of the population group */
    //@Column(name = "population_name", unique = true, nullable = false, length=25)
    private String name;
    
    /* Whether the population group is enabled */
    //@Column(name = "enabled", nullable = false)
    private boolean enabled = true;

    public static final PopulationGroup EMPTY = new PopulationGroup();
    public static final PopulationGroup WHITE = new PopulationGroup("WHITE");
    public static final PopulationGroup BLACK = new PopulationGroup("BLACK");
    public static final PopulationGroup COLORED = new PopulationGroup("COLORED");
    public static final PopulationGroup INDIAN = new PopulationGroup("INDIAN");
    public static final PopulationGroup ASIAN = new PopulationGroup("ASIAN");
    public static final PopulationGroup ARABIC = new PopulationGroup("ARABIC");
    public static final PopulationGroup ETHNIC = new PopulationGroup("ETHNIC");

    /**
     * Default constructor
     */
    public PopulationGroup() {
        super();
    }
    
    /**
     * Constructs the population group
     * @param name The name of the population group
     */
    public PopulationGroup(String name) {
        super();
        this.name = name;
    }
    
    /**
     * The unique id of the population group
     * @return Long
     */
	@Override
    public Long getId() {
		return this.id;
	}
	
    /**
     * The name of the population group
     * @return String
     */
	@Override
    public String getName() {
		return this.name;
	}

    /**
     * Whether the population group is enabled
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * Saves the gender if not already present in the persistent store
     */
    public PopulationGroup save() throws SaveException {

        /*
         * Persist the gender
         */
        try {
            List values = getDao().retrieve(Query.forClass(PopulationGroup.class).add(SimpleExpression.ilike("name", getName())));
            if (values != null && values.size() == 1) {
                PopulationGroup g = (PopulationGroup) values.get(0);
                this.id = g.getId();
            } else if (values != null && values.size() == 0) {
                PopulationGroup g = (PopulationGroup) getDao().persist(this);
                this.id = g.getId();
            } else if (values != null && values.size() > 1) {
                throw new SaveException("Multiple population groups found for name " + getName());
            } else {
                throw new SaveException("Null values returned from retrieve query");
            }
            return this;
        } catch (ServiceException e) {
            throw new SaveException("Unable to save population group", e);
        }
    }

    /**
     * Returns the name as the representation of the population group
     * @return String
     */
    @Override
    public String toString() {
        return getName();
    }
    
    /**
     * &lt;population_group class="xperiment.metaphor.model.people.PopulationGroup" name="WHITE"/&gt;
     * @return
     */
    public String toXML() {
         return "<population_group class='" + getClass().getName() 
                + "' id='" + getId() 
                + "' name='" + getName() 
                + "' enabled='" + isEnabled() 
                + "' />";
    }

    /**
     * Calculates equality based on the name of the population group
     * @param obj The other object
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PopulationGroup other = (PopulationGroup) obj;
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    /**
     * Calculates the hash code based upon the name of the population group
     * @return int
     */
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }
}
